package com.leyou.order.web;

import com.leyou.order.dto.OrderDTO;
import com.leyou.order.enums.PayState;
import com.leyou.order.pojo.Order;
import com.leyou.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    @PostMapping
    public ResponseEntity<Long> createOrder(@RequestBody OrderDTO orderDTO) throws Exception {
        Long spuId = this.orderService.createOrder(orderDTO);
        return ResponseEntity.ok(spuId);
    }

    @GetMapping("{id}")
    public ResponseEntity<Order> queryOrderById(@PathVariable("id") Long id) {
        Order order = this.orderService.queryOrderById(id);
        return ResponseEntity.ok(order);
    }

    @GetMapping("/url/{id}")
    public ResponseEntity<String> createPayUrl(@PathVariable("id") Long orderId) {
        String url = orderService.createPayUrl(orderId);
        return ResponseEntity.ok(url);
    }

    @GetMapping("/state/{id}")
    public ResponseEntity<PayState> queryOrderState(@PathVariable("id") Long orderId) {
        PayState status = this.orderService.queryOrderStatus(orderId);
        return ResponseEntity.ok(status);
    }


}
